package com.trainoo.nio.file;

import com.trainoo.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.Random;

/**
 * <p> NIO写入文件 </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2020/6/18 16:37
 */
@Slf4j
public class WriterFile2 {


    public static void main(String[] args) throws Exception {
        String[] accountId = "484680234180804608,482588846337294337,484679504908779521".split(",");
        String[] userId = "482556353710129155,482556353710129153,482556353710129154".split(",");

        File file = new File("C:\\Users\\TJ_tr\\Desktop\\test4.csv");
        if (file.exists()){
            log.info("delete file {}", file.delete());
        }

        long start = System.currentTimeMillis();
        Random random = new Random();
        // 指定文件写入类型append为true, 追加文件内容
        try (PrintStream ps = new PrintStream(new FileOutputStream(file), true)){
            String str = 10000 + random.nextInt(9999) + "";
            long bs = 10000000L;
            long loop = 200000L;
            for (long i = 0; i < loop; i ++){
                long tmp = bs + i;
                StringBuilder sb = new StringBuilder().append(accountId[(int) (i%3)]).append(",")
                        .append(userId[(int) (i%3)]).append(",").append(random.nextInt(13)).append(",").append("null").append(",")
                        .append(random.nextInt(1000)).append(",").append(str).append(tmp).append(",")
                        .append(DateUtil.getDateFormat(new Date(), DateUtil.YMD24H_DATA)).append(",")
                        .append(DateUtil.toDay());
                ps.println(sb.toString());
            }
        }
        long expendTime = System.currentTimeMillis() - start;
        log.info("success in [{}] milliseconds", expendTime);
        log.info("success in [{}] second", expendTime / 1000);
    }
}

/* // 10万条记录
2020-06-24 20:18:54 [com.trainoo.nio.file.WriterFile2] [INFO] - success in [2577] milliseconds
2020-06-24 20:18:54 [com.trainoo.nio.file.WriterFile2] [INFO] - success in [2] second
 */
/* // 100万条记录
2020-06-24 20:17:56 [com.trainoo.nio.file.WriterFile2] [INFO] - success in [9132] milliseconds
2020-06-24 20:17:56 [com.trainoo.nio.file.WriterFile2] [INFO] - success in [9] second
 */
/* // 500万条记录
2020-06-24 20:20:06 [com.trainoo.nio.file.WriterFile2] [INFO] - success in [48197] milliseconds
2020-06-24 20:20:06 [com.trainoo.nio.file.WriterFile2] [INFO] - success in [48] second
 */